package com.snpic.appaw.module.third.service.commonCertification;

import com.alibaba.fastjson.JSON;
import com.snpic.appaw.module.third.api.commonCertification.dto.RealNameVerificationReqDTO;
import com.snpic.appaw.module.third.api.commonCertification.dto.RealNameVerificationRspDTO;
import com.snpic.appaw.module.third.api.deepOCR.dto.VehicleLicenseReqDTO;
import com.snpic.appaw.module.third.api.deepOCR.dto.VehicleLicenseRespDTO;
import com.snpic.appaw.module.third.config.CommonCertificationConfig;
import com.snpic.appaw.module.third.service.httpclient.HttpClientServerImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.third.enums.ErrorCodeConstants.HTTPCLIENT_REQUEST_FAILED;

/**
 * @author JiJun
 * @date 2025/9/18 18:18
 * @修改时间 2025/9/18 18:18
 * @description
 */
@Service
@Slf4j
public class CommonCertificationServiceImpl implements CommonCertificationService {
	@Resource
	private CommonCertificationConfig commonCertificationConfig;

	@Resource
	private HttpClientServerImpl httpClientServer;

	/**
	 * 统一配置化实名验真, 各渠道场景实名验真
	 *
	 * @param reqDTO
	 * @return
	 */
	@Override
	public RealNameVerificationRspDTO realNameVerification(RealNameVerificationReqDTO reqDTO) {
		// 1. 查教第三方接口地址
		String url = commonCertificationConfig.getUrl();
		//2. 请求体补充完整.
		reqDTO.setSystemSource(commonCertificationConfig.getSystemSource());
		reqDTO.setTransactionCertificateNo(commonCertificationConfig.getTransactionCertificateNo() );
		//3.发送HTTP请求,调用第三方接口
		String postJson= null;
		try {
			postJson = httpClientServer.doPostJson(url, reqDTO, null, "统一配置化实名验真平台", "各渠道场景实名验真接口");
		} catch (HttpClientServerImpl.HttpClientException e) {
			log.error("第三方接口调用失败,统一配置化实名验真,各渠道场景实名验真", e);
			throw exception(HTTPCLIENT_REQUEST_FAILED);
		}
		//4. 解析返回结果
		RealNameVerificationRspDTO realNameVerificationRspDTO = JSON.parseObject(postJson, RealNameVerificationRspDTO.class);

		return realNameVerificationRspDTO;
	}
}
